package com.sky.service.Impl;

import com.github.pagehelper.Page;
import com.sky.Result.PageResult;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.mapper.ShopMapper;
import com.sky.mapper.UserBehaviorMapper;
import com.sky.model.dto.ShopPageQueryDTO;
import com.sky.model.dto.UserBehaviorDTO;
import com.sky.model.entity.UserBehavior;
import com.sky.model.vo.ShopVO;
import com.sky.service.UserBehaviorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserBehaviorServiceImpl implements UserBehaviorService {
    @Autowired
    UserBehaviorMapper userBehaviorMapper;
@Autowired
ShopMapper shopMapper;
    /**
     * 添加收藏功能
     * @param userBehaviorDTO
     */
    @Override
    public void bookmark(UserBehaviorDTO userBehaviorDTO) {
        //判断数据库是否为空
        UserBehavior userBehavior = new UserBehavior();
        //复制属性
        BeanUtils.copyProperties(userBehaviorDTO, userBehavior);
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //并设置用户id
        userBehavior.setUserId(userId);
        //设置更新时间
        userBehavior.setUpdateTime(LocalDateTime.now());
        //查询收藏
        UserBehavior bookmark = userBehaviorMapper.userBehavior(userId, userBehavior.getShopId());
        //判断添加收藏还是取消收藏
        //添加收藏
        if (userBehavior.getBookMark() == MessageConstant.DISABLE) {
            //判断是否为空
            if (bookmark == null) {
                //设置创建时间
                userBehavior.setCreateTime(LocalDateTime.now());
                //判断曾经是否已经收藏/浏览，如果没有则直接插入
                userBehaviorMapper.insertBookmark(userBehavior);
            }
            //添加收藏
            userBehaviorMapper.updateBookmark(1, userId, userBehavior.getShopId());
        }
        //取消收藏
        if (userBehavior.getBookMark() == MessageConstant.ENABLE) {
            userBehaviorMapper.updateBookmark(0, userId, userBehaviorDTO.getShopId());
        }
    }

    /**
     * 查询收藏功能
     *
     * @return
     */
    @Override
    public PageResult list(ShopPageQueryDTO shopPageQueryDTO) {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
    /*    PageHelper.startPage(shopPageQueryDTO.getPage(), shopPageQueryDTO.getPageSize());
        Page<ShopVO> page = shopMapper.pageQuery(shopPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());*/
        //查询收藏
        Page<ShopVO> page = shopMapper.starsList(userId);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 添加浏览量
     * @param userBehaviorDTO
     */
    @Override
    public void addViews(UserBehaviorDTO userBehaviorDTO) {
        //判断数据库是否为空
        UserBehavior userBehavior = new UserBehavior();
        //复制属性
        BeanUtils.copyProperties(userBehaviorDTO, userBehavior);
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //并设置用户id
        userBehavior.setUserId(userId);
        //设置更新时间
        userBehavior.setUpdateTime(LocalDateTime.now());
        //查询浏览
        UserBehavior views = userBehaviorMapper.userBehavior(userId, userBehaviorDTO.getShopId());
        //判断添加浏览量
        if (views == null) {
            //设置创建时间
            userBehavior.setCreateTime(LocalDateTime.now());
            //判断曾经是否已经收藏/浏览，如果没有则直接插入
            userBehaviorMapper.addViews(userBehavior);
        }
        //添加浏览量
        userBehavior.setViews(views.getViews() + 1);
        //更新浏览量
        userBehaviorMapper.updateViews(userBehavior);
    }

    /**
     * 查询浏览历史
     *
     * @return
     */
    @Override
    public PageResult history(ShopPageQueryDTO shopPageQueryDTO) {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //查询浏览历史
        Page<ShopVO> page = shopMapper.history(userId);
        return new PageResult(page.getTotal(), page.getResult());
    }
}




